//
//  ReplyTableView.m
//  yunqiphone
//
//  Created by Maple on 14-3-17.
//  Copyright (c) 2014年 ci123.com. All rights reserved.
//

#import "ReplyTableView.h"
#import "ReplyCell.h"
#import "ReplyModel.h"

@implementation ReplyTableView

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        // Initialization code
        _isAscending = YES;
        _replyTotal = 0;
    }
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"replyCell";
    ReplyCell *cell = [self dequeueReusableCellWithIdentifier:identify];
    if(cell == nil) {
        cell = [[ReplyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.backgroundColor = [UIColor clearColor];

        UIImageView *back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
        back.frame = CGRectMake(10, 0, 300, 75);
        back.backgroundColor = [UIColor whiteColor];
        cell.backgroundView = back;
    }

    cell.data = self.data[indexPath.section];
    NSInteger floor = 0;
    if (_isAscending) {
        floor = indexPath.section + 1;
    }else {
        floor = _replyTotal - indexPath.section;
    }
    cell.floor = [NSString stringWithFormat:@"%ld", (long)floor];
    cell.avatar_header = _avatar_header;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  int total_height = 92;
  int quote_heiget = 0;
  
  NSString *imageUrl=[self.data[indexPath.section] replyUserPic];
  NSString *content=[self.data[indexPath.section] replyContent];
  NSInteger quoteUserId=[[self.data[indexPath.section] replyQuoteUid] integerValue];
  //NSString *quoteImageUrl=[self.data[indexPath.section] replyQuotePic];
  NSString *quoteContent=[self.data[indexPath.section] replyQuoteContent];
  
  UIFont *font = [UIFont fontWithName:@"Arial" size:16];
  CGSize size = CGSizeMake(ContentWidth, 2000);
    CGSize labelsize = [content sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
  
  total_height += labelsize.height + 20;
  
    if(imageUrl!=[NSNull null] &&![imageUrl isEqualToString:@"<null>"]){
  if(imageUrl.length>5){
      int reW = [[NSString stringWithFormat:@"%@", [self.data[indexPath.section] replyUserPicWidth]] intValue];
        int reH = [[NSString stringWithFormat:@"%@", [self.data[indexPath.section] replyUserPicHeight]] intValue];
      
        float pic_w = ContentWidth - 10;
        float pic_h = pic_w;
        if(reW != 0 && reH != 0){
            pic_h = (int)(pic_w * reH / reW);
        }
        total_height += pic_h + 25;
  }
    }
  
  if(quoteUserId>0){ //引用回复
    quote_heiget += 70; //昵称高度
    UIFont *font2 = [UIFont fontWithName:@"Arial" size:14];
    CGSize size2 = CGSizeMake(ContentWidth - 20, 2000);
      CGSize labelsize2 = [quoteContent sizeWithFont:font2 constrainedToSize:size2 lineBreakMode:NSLineBreakByWordWrapping];
    
    quote_heiget += labelsize2.height + 10; //引用内容高度
    
//    if(quoteImageUrl.length>5){ //引用图片高度
//      if([quoteImageUrl rangeOfString:@"/face/"].location!=NSNotFound){//64*24 || 76*42
//          NSUInteger location = [quoteImageUrl rangeOfString:@"gif"].location;
//          if (location != NSNotFound) {
//              quote_heiget += 6 + 15;
//          }else {
//              quote_heiget += 10 + 15;
//          }
//      }else{
//          quote_heiget += ContentWidth - 20;
//      }
//    }
  }
  
  total_height+=quote_heiget;
  
  return total_height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APP_SCREEN_WIDTH, 4)];
    //[view setHidden:YES];
    [view setBackgroundColor:DefaultViewBackgroundColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
  return 4;
}
@end
